package com.css.fxfzypg.zcpt.sys.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.constants.FxfzConstants;
import com.css.fxfzypg.util.BuildUrl;
import com.css.fxfzypg.util.CreateGson;
import com.css.fxfzypg.util.RestTemplateUtil;
import com.css.fxfzypg.zcpt.sys.service.SysAreaService;
import com.google.gson.Gson;
import org.slw.common.helper.PropsHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

@Service
public class SysAreaServiceImpl implements SysAreaService {

    @Autowired
    private RestTemplateUtil restTemplateUtil;

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public String jsonSAreaTree(String areaId) {
        //读取配置文件
        Properties configGroup = PropsHelper.loadProps("slw-config.properties");
        String sysId = configGroup.getProperty("app.system.id");
        String url = configGroup.getProperty("baseRestUrl") + "/sorg_dzj/getDivisionTree?id="+areaId;
        Map<String, Object> paramMap = new HashMap<>(2);
        paramMap.put("id",areaId);
        ResponseEntity<String> responseEntity = restTemplateUtil.sendHttpRequest(url, paramMap);
        return  responseEntity.getBody();
    }

    @Override
    public String getChildAreaById(String divisionId) {
        Properties configGroup = PropsHelper.loadProps("slw-config.properties");
        String sysId = configGroup.getProperty("app.system.id");
        String url = configGroup.getProperty("baseRestUrl") + "/sorg_dzj/getChildDivisionById?id="+divisionId;
        Map<String, Object> paramMap = new HashMap<>(1);
        paramMap.put("id",divisionId);
        ResponseEntity<String> responseEntity = restTemplateUtil.sendHttpRequest(url, paramMap);
        return  responseEntity.getBody();
    }

    @Override
    public List getAllDivisionCode() {
        Gson gson = CreateGson.createGson();
        String jsonStr = redisTemplate.opsForValue().get(FxfzConstants.CACHE_SYS_KEY  + "areaList");
        List sysAreaList = gson.fromJson(jsonStr, List.class);
        if(sysAreaList==null || sysAreaList.size()==0){
            String url = BuildUrl.buildUrl() + "/sorg_dzj/getAllDivisionCode";
            Map<String, Object> paramMap = new HashMap<>(1);
            ResponseEntity<String> responseEntity = restTemplateUtil.sendHttpRequest(url, paramMap);
            sysAreaList = CreateGson.createGson().fromJson(responseEntity.getBody(), List.class);
            redisTemplate.opsForValue().set(FxfzConstants.CACHE_SYS_KEY +  "areaList",CreateGson.createGson().toJson(sysAreaList));
            return  sysAreaList;
        }
        return sysAreaList;
    }

    @Override
    public String getDivisionCodeById(String divisionId) {
        String url = BuildUrl.buildUrl() + "/sorg_dzj/getDivisionCodeById?divisionId="+divisionId;
        Map<String, Object> paramMap = new HashMap<>(1);
        paramMap.put("divisionId",divisionId);
        ResponseEntity<String> responseEntity = restTemplateUtil.sendHttpRequest(url, paramMap);
        return responseEntity.getBody();
    }

    @Override
    public String getIdByCondition(String province, String city, String area) {
        String url = BuildUrl.buildUrl() + "/sorg_dzj/getIdByCondition?province="+province+"&city="+city+"&area="+area;
        Map<String, Object> paramMap = new HashMap<>(3);
        paramMap.put("province",province);
        paramMap.put("city",city);
        paramMap.put("area",area);
        ResponseEntity<String> responseEntity = restTemplateUtil.sendHttpRequest(url, paramMap);
        return responseEntity.getBody();
    }

    @Override
    public String getAreaCodeByUserId(String userId) {
        String url = BuildUrl.buildUrl() + "/suser_dzj/getAreaCodeByUserId?userId="+userId;
        Map<String, Object> paramMap = new HashMap<>(1);
        paramMap.put("userId",userId);
        ResponseEntity<String> responseEntity = restTemplateUtil.sendHttpRequest(url, paramMap);
        return  responseEntity.getBody();
    }
}
